package org.jeecg.modules.wuser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.modules.inquiry.service.IInquiryInfoService;
import org.jeecg.common.modules.order.entity.OrderInfo;
import org.jeecg.common.modules.order.service.IOrderInfoService;
import org.jeecg.common.modules.patient.entity.Patient;
import org.jeecg.common.modules.patient.service.IPatientService;
import org.jeecg.common.modules.wx.entity.WxUserinfo;
import org.jeecg.common.modules.wx.service.IWxUserinfoService;
import org.jeecg.common.modules.wx.vo.WxUserInfoReqVo;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.data.service.PageReturn;
import org.jeecg.modules.system.vo.OrderInfoVo;
import org.jeecg.modules.system.vo.WuserOrderInfoResponseVo;
import org.jeecg.modules.system.vo.ResultVo;
import org.jeecg.modules.wuser.vo.PatientInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Api(tags="wx_userinfo")
@RestController
@RequestMapping("/wuser/wxUserinfo")
@Slf4j
public class WxUserinfoController extends JeecgController<WxUserinfo, IWxUserinfoService> {
	@Autowired
	private IWxUserinfoService wxUserinfoService;

	@Resource
	private IOrderInfoService orderInfoService;

	@Resource
	private IPatientService iPatientService;

	@Resource
	private IInquiryInfoService inquiryInfoService;

	@AutoLog(value = "wx_userinfo-分页列表查询")
	@ApiOperation(value="wx_userinfo-分页列表查询", notes="wx_userinfo-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(WxUserinfo wxUserinfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		//数据开始的记录数
		int dataStart = (pageNo - 1) * pageSize;

		//先查询所有的用户信息
		String nickname = req.getParameter("nickname");
		String mobile   = req.getParameter("mobile");
		String doctorName = req.getParameter("doctorName");
		List<WxUserInfoReqVo> wxUserinfos = wxUserinfoService.getWxUserInfos(nickname,mobile,doctorName,dataStart,pageSize);

		//查询到的总数据
		Integer count = wxUserinfoService.getCount(nickname,mobile,doctorName);

		BigDecimal amount = new BigDecimal(0.00);  //总金额
		int orderAll  = 0;  //总订单数
		int orderIng  = 0;  //进行中的订单
		List responseInfo = new ArrayList();          //定义新组合名称

		OrderInfoVo orderInfoVo = new OrderInfoVo();  //组合计算出的：总金额、总订单数、进行中的订单

		for(WxUserInfoReqVo userInfoOne : wxUserinfos){
			//查询该用户的所有订单信息
			List<OrderInfo> orderInfos = orderInfoService.getOrderInfo(userInfoOne.getId());

			//设置金额、总订单数、进行中订单数
			for(OrderInfo orderInfoOne : orderInfos){
				amount = amount.add(orderInfoOne.getAmount()); //消费金额
				orderAll ++;
				if(orderInfoOne.getStatus() == 1){
					orderIng ++;
				}
				orderInfoVo.setAmount(amount);
				orderInfoVo.setOrderAll(orderAll);
				orderInfoVo.setOrderIng(orderIng);
			}
			WuserOrderInfoResponseVo vo = this.initWuserOrderInfoResponseVo(userInfoOne,orderInfoVo);
			responseInfo.add(vo);
		}

		//组合分页展示的数据
		PageReturn pageReturn = new PageReturn();
		ResultVo vo = pageReturn.initResultVo(responseInfo,count,pageNo,pageSize);

		return Result.ok(vo);
	}

	/**
	 * 组合用户和订单的信息
	 * @param wxUserinfo
	 * @param orderInfoVo
	 * @return
	 */
	private WuserOrderInfoResponseVo initWuserOrderInfoResponseVo(WxUserInfoReqVo wxUserinfo ,OrderInfoVo orderInfoVo){
		WuserOrderInfoResponseVo vo = new WuserOrderInfoResponseVo();
		vo.setId(wxUserinfo.getId());
		vo.setNickname(wxUserinfo.getNickname());
		vo.setMobile(wxUserinfo.getMobile());
		vo.setAmount(orderInfoVo.getAmount());
		vo.setOrderAll(orderInfoVo.getOrderAll());
		vo.setOrderIng(orderInfoVo.getOrderIng());
		return vo;
	}

	@AutoLog(value = "wx_userinfo-添加")
	@ApiOperation(value="wx_userinfo-添加", notes="wx_userinfo-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody WxUserinfo wxUserinfo) {
		wxUserinfoService.save(wxUserinfo);
		return Result.ok("添加成功！");
	}

	@AutoLog(value = "wx_userinfo-编辑")
	@ApiOperation(value="wx_userinfo-编辑", notes="wx_userinfo-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody WxUserinfo wxUserinfo) {
		wxUserinfoService.updateById(wxUserinfo);
		return Result.ok("编辑成功!");
	}

	@AutoLog(value = "wx_userinfo-通过id删除")
	@ApiOperation(value="wx_userinfo-通过id删除", notes="wx_userinfo-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id") String id) {
		wxUserinfoService.removeById(id);
		return Result.ok("删除成功!");
	}

	@AutoLog(value = "wx_userinfo-批量删除")
	@ApiOperation(value="wx_userinfo-批量删除", notes="wx_userinfo-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.wxUserinfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}


	@AutoLog(value = "wx_userinfo-通过id查询")
	@ApiOperation(value="wx_userinfo-通过id查询", notes="wx_userinfo-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id") String id) {
		WxUserinfo wxUserinfo = wxUserinfoService.getById(id);
		if(wxUserinfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(wxUserinfo);
	}

	@AutoLog(value = "wx_userinfo-通过id查询")
	@ApiOperation(value="wx_userinfo-通过id查询", notes="wx_userinfo-通过id查询")
	@GetMapping(value = "/queryByUserInfo")
	public Result<?> queryByUserInfo(@RequestParam(name="id") String id) {
		//当前用户的信息
		WxUserinfo wxUserinfo = wxUserinfoService.getById(id);
		//获取用户名下患者信息
		List<Patient> patients = iPatientService.getByWxUserInfoId(id);
		List patientInfoArr = new ArrayList();
		for(Patient patient:patients){
			int num = inquiryInfoService.getInquiryNum(patient.getId());
			PatientInfo vo = this.initPatientInfo(patient,num);
			patientInfoArr.add(vo);
		}

		List info = new ArrayList();
		info.add(wxUserinfo);
		info.add(patientInfoArr);
		return Result.ok(info);
	}

	/**
	 * 组合患者信息
	 * @return
	 */
	private PatientInfo initPatientInfo(Patient patient , Integer num){
		PatientInfo vo = new PatientInfo();
		vo.setId(patient.getId());
		vo.setName(patient.getName());
		vo.setRelation(patient.getRelation());
		vo.setInquiryNum(num);
		return vo;
	}


	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, WxUserinfo wxUserinfo) {
		return super.exportXls(request, wxUserinfo, WxUserinfo.class, "wx_userinfo");
	}


	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, WxUserinfo.class);
	}

}
